package com.android.jingweiapp.fragment.interaction;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.OnlineConsultationXMLPhase;
import com.android.jingweiapp.activity.SearchActivity;
import com.android.jingweiapp.adapter.ConsultationAdapter;
import com.android.jingweiapp.bean.ConsultationBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;
import com.android.jingweiapp.widget.MyToast;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 网上咨询
 */
public class ConsultFragment extends Fragment {

    /*@Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.btn_search)
    TextView btnSearch;*/
    @Bind(R.id.list_news)
    ListView listNews;

    private ArrayList<ConsultationBean.DataEntity> newsBeans;
    private ConsultationAdapter adapter;


    public ConsultFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_consult, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    public void init() {


        newsBeans = new ArrayList<>();
        adapter = new ConsultationAdapter(newsBeans, getActivity());
        listNews.setAdapter(adapter);

        MyAsyncTask myAsynctask = new MyAsyncTask();
        myAsynctask.execute();

    }


    private void dealData(String result) {
        if (TextUtils.isEmpty(result)) {
            MyToast.getInstance().show("出错了，请重试");
            return;
        }
        if (result.equals("nodata")) {
            if (adapter.getCount() > 0) {
                MyToast.getInstance().show("出错了，请重试");
            } else {
                MyToast.getInstance().show("无更多数据");
            }
            return;
        }
        Gson gson = new Gson();
        ConsultationBean consultationBean = gson.fromJson(result, ConsultationBean.class);
        newsBeans.addAll(consultationBean.getData());
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class MyAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String[] params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    OnlineConsultationXMLPhase.createParam(getActivity()),
                    new OnlineConsultationXMLPhase());
        }

        @Override
        protected void onPostExecute(String str) {
            super.onPostExecute(str);
            dealData(str);
        }
    }
}
